package ase.assignment.sts.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ase.assignment.sts.api.CustomerHandler;
import ase.assignment.sts.api.HandlerFactoryProxy;
import ase.assignment.sts.beans.CustomerEntity;
import ase.assignment.sts.utils.STSConstants;

public class AccountAction extends GenericAction {
	public static final int KEY_SUCCESS = 1;
	public static final int KEY_FAIL = 2;

	public int process(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String username = (String) request.getSession().getAttribute(
				STSConstants.SESSION_USER);
		if (username == null) {
			return KEY_FAIL;
		}
		String sPassword = request.getParameter("password");
		String sEmail = request.getParameter("email");

		CustomerHandler handler = HandlerFactoryProxy.instance()
				.getCustomerHandler();
		try {
			CustomerEntity entity = handler.find(username);
			entity.setEmail(sEmail);
			entity.setPassword(sPassword);
			handler.update(entity);

			return KEY_SUCCESS;

		} catch (Throwable e) {
			e.printStackTrace();
		}

		return KEY_FAIL;
	}
}
